package com.pj.wisdomlogistics.ui.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.ViewGroup;
import android.widget.EditText;

import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.entity.response.FoodMenu;
import com.pj.wisdomlogistics.ui.view.recyclerview.BaseAdapter;
import com.pj.wisdomlogistics.ui.view.recyclerview.BaseViewHolder;
import com.pj.wisdomlogistics.utils.Log;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

/**
 * Created by pamper on 2018/2/26.
 */

public class MenuEditorAdapter extends BaseAdapter<List<FoodMenu> , BaseViewHolder> {
    private int menuType = 0;
    private int[] arrayMenu = new int[]{
            R.id.et_name1 , R.id.et_price1 , R.id.et_num1 ,
            R.id.et_name2 , R.id.et_price2 , R.id.et_num2 ,
            R.id.et_name3 , R.id.et_price3 , R.id.et_num3 ,
            R.id.et_name4 , R.id.et_price4 , R.id.et_num4 ,
            R.id.et_name5 , R.id.et_price5 , R.id.et_num5 ,
            R.id.et_name6 , R.id.et_price6 , R.id.et_num6 ,
    };

    public MenuEditorAdapter(Context context) {
        super(context);
    }

    public void setList(List<List<FoodMenu>> dataList , int menuType) {
        this.menuType = menuType;
        Log.e("Tag" , dataList.toString());
        super.setList(dataList);
    }

    @Override
    public BaseViewHolder createCustomViewHolder(ViewGroup parent, int viewType) {
        return new BaseViewHolder(parent, viewType == 0 ? R.layout.item_menu_noodle : R.layout.item_menu_cook);
    }

    @Override
    public void bindCustomViewHolder(BaseViewHolder viewHolder, int position) {
        AutoUtils.autoSize(viewHolder.itemView);

        viewHolder.setIsRecyclable(false);

        List<FoodMenu> item = getItem(position);
        int length = 0;
        if(menuType == 0){
            length = 6 ;
        }else if(menuType == 1){
            length = 3;
        }

        for (int i = 0; i < length; i++) {
            ((EditText)viewHolder.getView(arrayMenu[i * 3])).setText(item.get(i).getName());
            ((EditText)viewHolder.getView(arrayMenu[i * 3 + 1])).setText(item.get(i).getPrice() + "");
            ((EditText)viewHolder.getView(arrayMenu[i * 3 + 2])).setText(item.get(i).getCount() + "");
        }

        for (int i = 0; i < (menuType == 0 ? arrayMenu.length : 9); i++) {
            ((EditText)viewHolder.getView(arrayMenu[i])).addTextChangedListener(
                    new TextSwitcher(i , position, (EditText)viewHolder.getView(arrayMenu[i])));
        }

        String week = "";
        switch (position){
            case 0:
                week = "周一";
                break;
            case 1:
                week = "周二";
                break;
            case 2:
                week = "周三";
                break;
            case 3:
                week = "周四";
                break;
            case 4:
                week = "周五";
                break;
            case 5:
                week = "周六";
                break;
            case 6:
                week = "周日";
                break;
        }

        viewHolder.getTextView(R.id.tv_week).setText(week);
    }

    @Override
    public int getCustomViewType(int position) {
        return menuType;
    }


    //自定义EditText的监听类
    class TextSwitcher implements TextWatcher {
        private int tag;
        private int position;
        private EditText editText;

        public TextSwitcher(int tag , int position , EditText mEditText){
            this.tag = tag;
            this.position = position;
            this.editText = mEditText;

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            mOnEditor.editor(tag , position , editText.getText().toString());
        }
    }

    public onEditor mOnEditor;

    public interface onEditor {
        public void editor(int tag , int position , String msg);
    }

    public void setListener(onEditor mListener) {
        this.mOnEditor = mListener;
    }
}
